1Writer TC UI2
2024/10/20
どのノートからでも呼び出せるようにし、リストを閉じたら元のノートを開く
code:js
// 現在の日付を取得してデイリーノートのファイル名を決定
const today = new Date();
const year = today.getFullYear();
const month = ('0' + (today.getMonth() + 1)).slice(-2); // 月を2桁に
const day = ('0' + today.getDate()).slice(-2); // 日を2桁に
const dailyNoteFileName = ${year}-${month}-${day}.md;
// デイリーノートのファイルパスを決定
const folderPath = editor.getFolderPath();
const dailyNotePath = folderPath + "/" + dailyNoteFileName;
// 現在のノートのファイルパス
const fileName = editor.getFileName();
const fullFilePath = folderPath + "/" + fileName;
// デイリーノートの内容を取得してからリストを表示
editor.openFile(dailyNotePath, "edit", function(success) {
if (success) {
const noteText = editor.getText(); // ファイルの内容を取得
// 内容を元にリストを表示
reloadAndShowList(noteText); // ノートのテキストを渡してリスト表示
} else {
ui.hudError("デイリーノートが見つかりません");
}
});
// リスト表示・処理関数
function reloadAndShowList() {
let noteText = editor.getText();
// ノート全体の行を読み込み、インデックスを保持
let allLines = noteText.split('\n').filter(line => line.trim() !== ''); // 空行を除去
let indexedLines = allLines.map((line, index) => ({ line, index }));
// 正規表現にマッチしたタスクのみを処理、インデックスを保持
let tasks = indexedLines
.map(item => {
const match = item.line.match(/- \ \(?: (\d{2}:\d{2})-)?(?: \\(\d+))?(.*)|🗂️.*/); if (match) {
return { line: item.line, index: item.index, startTime: match1 || null }; // マッチした行とそのインデックス、開始時刻を保持 } else {
return null; // マッチしない行は無視
}
})
.filter(task => task !== null); // 無効な行を除外
if (tasks.length > 0) {
// 開始時刻が設定されているタスクとされていないタスクを分ける
let tasksWithTime = tasks.filter(task => task.startTime !== null);
let tasksWithoutTime = tasks.filter(task => task.startTime === null);
// 開始時刻のあるタスクを時刻順にソート
tasksWithTime.sort((a, b) => {
return (aHours * 60 + aMinutes) - (bHours * 60 + bMinutes);
});
// 時刻付きタスクと時刻なしタスクを結合
// 結果を格納するリスト
let endTimes = [];
let currentDateTime = new Date(); // 現在の時刻を基準にする
let lastEndTime = ''; // 最後のタスクの終了時刻を保持する変数
tasks.forEach((task, index) => {
let taskLine = task.line;
// 🗂️で始まる行を処理
if (taskLine.startsWith("🗂️")) {
endTimes.push(taskLine); // タスク名をそのままリストに追加
return; // 次のループへ
}
// タスクから開始時刻、所要時間、タスク名を抽出
let match = taskLine.match(/- \ \(?: (\d{2}:\d{2})-)?(?: \\(\d+))?(.*)/); if (!match) {
return; // 次のタスクへ
}
let startTime = match1; // 開始時刻 (optional) let duration = match2 ? parseInt(match2, 10) : 0; // 所要時間 (デフォルトで0) let taskName = match3.trim(); // タスク名 let baseTime;
let actualStartTime;
// ui.alert(taskName + ' ' + startTime)
if (startTime) {
baseTime = new Date();
baseTime.setHours(hours);
baseTime.setMinutes(minutes);
baseTime.setSeconds(0);
baseTime.setMilliseconds(0);
} else {
baseTime = new Date(currentDateTime);
}
actualStartTime = new Date(baseTime); // 実際の開始時刻
let endDate = new Date(baseTime.getTime() + duration * 60000);
if (endDate < new Date()) {
endDate = new Date();
}
let startTimeStr = actualStartTime.toTimeString().slice(0, 5);
let endTimeStr = endDate.toTimeString().slice(0, 5);
lastEndTime = endTimeStr;
let taskStatus = ""
if (startTime) {
let elapsedTime = Math.ceil((new Date() - actualStartTime) / 60000);
if (elapsedTime > duration) {
let overdueMinutes = elapsedTime - duration;
taskStatus = 超過${overdueMinutes}分;
taskName = ⚠️ ${taskName}
} else {
let remainingTime = duration - elapsedTime;
taskStatus = 残り${remainingTime}分;
taskName = ➡️ ${taskName}
}
}
taskOutput = ${taskName}|${task.index}|${startTimeStr} - ${endTimeStr} (${duration}分)${taskStatus};
endTimes.push(taskOutput);
currentDateTime = endDate;
});
// メモ追加用の行を追加
endTimes.unshift('✏︎ 行間メモ|-2|');
// タスク追加・開始用の行を追加
let addstart = '➕ タスク追加開始|-1|';
endTimes.unshift(addstart);
// 結果をリスト形式で表示
ui.list(終了時刻: ${lastEndTime}, endTimes, (selectedIndex) => {
// ✏︎ 行間メモ処理
if (selectedIndex == -2) {
ui.input('✏︎ 行間メモ', null, '行間メモを入力', value => {
if (value) {
let now = new Date().toTimeString().slice(0, 5); // 現在時刻を取得
let newTask = - ${now} ${value};
allLines.push(newTask); // 新しいタスクをリストに追加
editor.setText(allLines.join('\n')); // ノート全体を更新
reloadAndShowList(); // リストを再表示
}
});
}
// ➕ タスク追加・開始処理
else if (selectedIndex == -1) {
ui.input('➕ タスク追加・開始', null, 'タスク名を入力', value => {
if (value) {
// 正規表現で末尾が " 数字" かを確認
const match = value.match(/(.*)\s+(\d+)$/);
let taskName, duration;
if (match) {
// マッチした場合、タスク名と所要時間に分割
taskName = match1.trim(); // タスク名 (数字以外の部分) duration = \\${match[2]} ; // 数字部分を\数字に変換
} else {
// マッチしない場合はそのままタスク名として扱う
taskName = value.trim();
duration = ''; // 所要時間なし
}
// 新しいタスクをリストの末尾に追加
let now = new Date().toTimeString().slice(0, 5); // 現在時刻を取得
let newTask = - [ ] ${now}- ${duration}${taskName};
allLines.push(newTask); // 新しいタスクをリストに追加
editor.setText(allLines.join('\n')); // ノート全体を更新
reloadAndShowList(); // リストを再表示
}
});
}
// 選択されなかった場合の処理
else if (selectedIndex === null || selectedIndex === undefined) {
// ファイルを再度開く
editor.openFile(fullFilePath, "edit", function(success) {
if (success) {
// ui.hudSuccess("ノートを再度開きました");
} else {
ui.hudError("元のノートを開けませんでした");
}
});
return;
}
else {
let now = new Date().toTimeString().slice(0, 5);
let updatedLine;
if (/^- \ \(?! \d{2}:\d{2}-)/.test(currentLineText)) { updatedLine = currentLineText.replace("- ", - [ ] ${now}-);
} else if (/^- \ \ \d{2}:\d{2}-/.test(currentLineText)) { updatedLine = currentLineText.replace(/^-\s\ \\s(\d{2}:\d{2})-/, - [x] $1-${now}); }
editor.setText(allLines.join('\n'));
reloadAndShowList();
}
});
} else {
ui.alert("未完了タスクが見つかりませんでした。");
}
}